package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Classinfo;
import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.ClassInfoQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IClassinfoService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Classinfo)表控制层
 *
 * @author makejava
 * @since 2020-06-19 17:17:43
 */
@Controller
@RequestMapping("/classinfo")
public class ClassinfoController {

    @Resource
    private IClassinfoService IClassinfoService;
    @Resource
    private IEmployeeService employeeService;

    /**
     * 页面展示请求
     *
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("/list")
    @RequiredPermission(name = "班级列表", expression = "classinfo:list")
    public String List(Model model, @ModelAttribute("qo") ClassInfoQueryObject qo) {
        PageInfo<Classinfo> pageResult = IClassinfoService.query(qo);
        model.addAttribute("pageResult",pageResult);


        List<Employee> employees=employeeService.listAll();
        model.addAttribute("employees",employees);


        return "classinfo/list";
    }

    //处理删除请求
    @RequiredPermission(name = "班级删除", expression = "classinfo:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        try {
            if (id != null) {
                IClassinfoService.deleteById(id);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "删除失败");
        }

    }




    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Classinfo classinfo) {
        try {
            if ( classinfo.getId() != null) {
                IClassinfoService.update( classinfo);
            } else {
                IClassinfoService.insert( classinfo);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "操作失败");
        }

    }
}